package com.woniuxy.cardweb.controller;


import com.woniuxy.cardservice.service.CountCardService;
import com.woniuxy.core.exception.ParameterValidException;
import com.woniuxy.core.model.Result;
import com.woniuxy.core.util.MyJwtUtil;
import com.woniuxy.model.dto.CountCardDto;
import com.woniuxy.model.form.AddCountCardForm;
import com.woniuxy.model.form.UpdateCountCardForm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 次卡会员表 前端控制器
 * </p>
 *
 * @author 冉杰
 * @since 2024-01-15
 */
@Api(tags = {"次卡会员卡资源接口"})
@RestController
@RequestMapping("/countCard")
public class CountCardController {
    public static void main(String[] args) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("venueId",1);
        String token = MyJwtUtil.createVenueToken(map, 100);
        System.out.println(token);
    }
    @Autowired
    private CountCardService countCardService;


    /**
     * 商家端新增次卡
     * @param form 新增次卡参数对象
     * @param token 商家登录token
     * @return
     */
    @ApiOperation("商家端次卡新增")
    @PostMapping("/add.venue")
    public Result<CountCardDto> addCountCard(@RequestBody @Validated AddCountCardForm form,@RequestHeader String token){
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        CountCardDto dto = countCardService.addCountCard(form,venueId);
        return Result.success("新增次卡成功",dto);
    }

    /**
     * 商家修改次卡信息
     * @param form
     * @param token 商家登录token
     * @return
     */
    @ApiOperation("商家端修改次卡信息")
    @PostMapping("/update.venue")
    public Result updateCountCard(@RequestBody @Validated UpdateCountCardForm form,@RequestHeader String token){
         Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        countCardService.updateCountCard(form,venueId);
        return Result.success("商家端修改次卡信息成功");
    }

    /**
     * 商家端禁用次卡
     * @param cardId 次卡id
     * @param token 商家登录token
     * @return
     */
    @ApiOperation("商家端禁用次卡")
    @GetMapping("/disabled.venue")
    public Result disabledCountCard(Integer cardId,@RequestHeader String token){
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        countCardService.disabledCountCard(cardId,venueId);
        return Result.success("商家端禁用次卡成功");
    }
    /**
     * 商家端启用次卡
     * @param cardId 次卡id
     * @param token 商家登录token
     * @return
     */
    @ApiOperation("商家端启用次卡")
    @GetMapping("/enable.venue")
    public Result enableCountCard(Integer cardId,@RequestHeader String token){
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        countCardService.enableCountCard(cardId,venueId);
        return Result.success("商家端启用次卡成功");
    }

    /**
     * 根据id查找次卡
     * @return
     */
    @ApiOperation("根据id查找次卡")
    @GetMapping("/findById")
    public Result<CountCardDto> findById(Integer cardId) {
        CountCardDto dto = countCardService.findById(cardId);
        return Result.success("根据id查找次卡", dto);
    }

    /**
    * 根据商家id查找次卡
    * @return
    */
    @ApiOperation("根据商家id查找次卡")
    @GetMapping("/findByVenueId")
    public Result<List<CountCardDto>> findByVenueId(Integer venueId){
       List<CountCardDto>  dtos = countCardService.findByVenueId(venueId);
       return Result.success("根据商家id查找次卡成功",dtos);
    }

    /**
     * 商家查看自家次卡
     * @return
     */
    @ApiOperation("商家查看自家次卡")
    @GetMapping("/venueList.venue")
    public Result<List<CountCardDto>> findByVenueId(@RequestHeader String token){
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        List<CountCardDto>  dtos = countCardService.findByVenueId(venueId);
        return Result.success("商家查看自家次卡",dtos);
    }
}

